package com.wpml.cloud.classroom.handler;


import cn.hutool.json.JSONUtil;
import com.wpml.cloud.classroom.msg.Message;
import com.wpml.cloud.classroom.netty.NettyServer;
import io.netty.channel.Channel;
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

/**
 * @Description: 消息发送管理器
 * @Author: ZhangLuo
 * @Email: 1946430@qq.com
 */

public class MessageManager {
    private static final Logger log = LogManager.getLogger(MessageManager.class);

    /**
     * 发送消息, 有日志输出
     * @param channel 发送目标
     * @param message 消息体
     */
    public static <T> void sendMessage(Channel channel, Message message) {
        sendMessageNoLog(channel, message);
        log.debug("消息发送成功, 目标用户: [{}], 消息内容: {}", "", message);
    }

    /**
     * 发送消息, 没有日志输出
     * @param channel
     * @param message
     */
    public static <T> void sendMessageNoLog(Channel channel, Message message) {

        channel.writeAndFlush(new TextWebSocketFrame(JSONUtil.toJsonStr(message)));
    }

}
